﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Linq.Expressions;

public partial class uitgebreidresultaat : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        string trefwoord = Request.QueryString["trefwoord"];
        string zoeken = Request.QueryString["zoeken"];
        string grootte = Request.QueryString["grootte"];
        string platform = Request.QueryString["platform"];

        Label3.Text += trefwoord;

        Label kopje = new Label();

        if (zoeken == "0")
        {
            zoekopdrachtTitel(trefwoord, grootte, platform);
            kopje.Text = "Gezocht in: Titels";
        }
        else
        {
            zoekopdrachtMaker(trefwoord, grootte, platform);
            kopje.Text = "Gezocht in: Ontwikkelaars";
        }
        paneluitgebreid.Attributes.Add("style", "font-weight:bold");
        paneluitgebreid.Controls.Add(kopje);
    }
    //pagina nummers regelen voor gridview2
    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void zoekopdrachtMaker(string trefwoord, string grootte, string platform)
    {
        var trefwoorden = ContainsMaker(trefwoord);
        var platforms = ContainsPlatform(platform);
        var groottes = ContainsGrootte(grootte);
        int counter = 0;
        var queryMaker =
                         from m in db.Makers.Where(trefwoorden)
                         group m by m.NAAM into m
                         select new { naam = m.Key, logo = m.Max(p => p.LOGO), maker_id = m.Max(p => p.MAKER_ID) };

        foreach (var x in queryMaker)
        { 
            var arrayMaker = queryMaker.ToArray();
            int maker_id = arrayMaker[counter].maker_id;
            counter++;

            Label Label1 = new Label();
            trefwoorden = ContainsMaker(x.naam);
            var zoekquery =
                    (from g in db.Games.Where(groottes)
                    join m in db.Makers.Where(trefwoorden) on g.MAKER_ID equals m.MAKER_ID
                    join p in db.Platforms.Where(platforms) on g.PLATFORM_ID equals p.PLATFORM_ID
                    select new { g.GAME_NR, g.TITEL, m.NAAM, p.OMSCHRIJVING, g.PRIJS,g.IMAGE}).Take(3).ToList();

            GridView grd = new GridView();
            grd.ID = x.naam;
            styleGridview(grd);
            grd.DataSource = zoekquery;

            string[] test = {"game_nr"};
            HyperLinkField artikelnaam = new HyperLinkField();
            artikelnaam.ControlStyle.Width = 200;
            artikelnaam.DataNavigateUrlFields = test;
            artikelnaam.DataNavigateUrlFormatString="Details.aspx?id={0}";
            artikelnaam.HeaderText="Artikelnaam";
            artikelnaam.DataTextField="Titel";

            BoundField platform1 = new BoundField();
            platform1.DataField = "omschrijving";
            platform1.HeaderText = "Platform";

            BoundField prijs = new BoundField();
            prijs.DataField = "prijs";
            prijs.HeaderText = "Prijs (€)";
           
            ImageField field = new ImageField();
            field.DataImageUrlField = "image"; 
            field.ControlStyle.Width = 30;
            field.ControlStyle.Height = 40;

            grd.Columns.Add(field);
            grd.Columns.Add(artikelnaam);
            grd.Columns.Add(platform1);
            grd.Columns.Add(prijs);

            Panel paneel = new Panel();
            paneel.BackColor = System.Drawing.Color.White;
            paneel.Attributes.Add("style", "margin-top:10px;margin-left:20px;margin-right:20px;");
            paneel.BorderStyle = BorderStyle.Solid;
            paneel.BorderWidth = 1;
            paneel.BorderColor = System.Drawing.Color.FromName("#576A92");

            Image logo = new Image();
            logo.ImageUrl = x.logo;
            logo.Width = 30;
            logo.Attributes.Add("style", "margin-top:3px;margin-left:15px;margin-bottom:3px;");
            logo.ImageAlign = ImageAlign.Top;
            paneel.Controls.Add(logo);
            
            HyperLink makernaam = new HyperLink();
            makernaam.ControlStyle.Width = 200;
            makernaam.Attributes.Add("style", "margin-left:10px;margin-top:8px;");
            makernaam.NavigateUrl = "Details_maker.aspx?id=" + maker_id;
            makernaam.Text = x.naam;
           
            paneel.Controls.Add(makernaam);

            PlaceHolder1.Controls.Add(paneel);

            grd.DataBind();
            PlaceHolder1.Controls.Add(grd);
        }
    }

    protected void styleGridview(GridView grd)
    {
        // Set the GridView object's properties. 
        grd.AutoGenerateColumns = false;
        grd.CellPadding = 4;
        grd.ForeColor = System.Drawing.Color.FromName("#333333");
        grd.GridLines = GridLines.None;
        grd.Attributes.Add("style", "margin-left:136px;");
        grd.Width = 714;
        grd.RowStyle.Wrap = false;
        grd.BorderStyle = BorderStyle.Solid;
        grd.BorderWidth = 1;
        grd.AllowPaging = false;
        grd.RowStyle.BackColor = System.Drawing.Color.FromName("#EFF3FB");
        grd.FooterStyle.BackColor = System.Drawing.Color.FromName("#507CD1");
        grd.FooterStyle.Font.Bold = true;
        grd.FooterStyle.ForeColor = System.Drawing.Color.White;
        grd.HeaderStyle.BackColor = System.Drawing.Color.FromName("#507CD1");
        grd.HeaderStyle.ForeColor = System.Drawing.Color.White;
        grd.HeaderStyle.Font.Bold = true;
        grd.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

        grd.AlternatingRowStyle.BackColor = System.Drawing.Color.White;

        grd.EditRowStyle.BackColor = System.Drawing.Color.FromName("#2461BF");

        grd.PagerStyle.BackColor = System.Drawing.Color.FromName("#2461BF");
        grd.PagerStyle.ForeColor = System.Drawing.Color.White;
        grd.PagerStyle.HorizontalAlign = HorizontalAlign.Center;

        grd.SelectedRowStyle.BackColor = System.Drawing.Color.FromName("#D1DDF1");
        grd.SelectedRowStyle.ForeColor = System.Drawing.Color.FromName("#333333");
        grd.SelectedRowStyle.Font.Bold = true;

    }
    protected void zoekopdrachtTitel(string trefwoord,string grootte,string platform)
    {
        var trefwoorden = ContainsTitel(trefwoord);
        var platforms = ContainsPlatform(platform);
        var groottes = ContainsGrootte(grootte);

        var zoekquery =
            from g in db.Games.Where(trefwoorden.And(groottes))
            join m in db.Makers on g.MAKER_ID equals m.MAKER_ID
            join p in db.Platforms.Where(platforms) on g.PLATFORM_ID equals p.PLATFORM_ID
            select new { g.MAKER_ID, g.GAME_NR, g.TITEL, m.NAAM, p.OMSCHRIJVING, g.PRIJS, g.IMAGE };
 
        GridView1.DataSource = zoekquery;
        GridView1.DataBind();
    }
    protected void zoekopdracht(string zoekTitel, string maker, string grootte, string platform)
    {

        var titels = ContainsTitel(zoekTitel);
        var makers = ContainsMaker(maker);
        var platforms = ContainsPlatform(platform);
        var groottes = ContainsGrootte(grootte);

        var zoekquery =
            from g in db.Games.Where(titels.And(groottes))
            join m in db.Makers.Where(makers) on g.MAKER_ID equals m.MAKER_ID
            join p in db.Platforms.Where(platforms) on g.PLATFORM_ID equals p.PLATFORM_ID
            select new { g.TITEL, m.NAAM, p.OMSCHRIJVING, g.PRIJS, g.IMAGE };

        GridView1.DataSource = zoekquery;
        GridView1.DataBind();

    }
    public static Expression<Func<Game, bool>> ContainsTitel(
                                                string keywords)
    {
        var predicate = PredicateBuilder.False<Game>();
        string[] woorden = keywords.Split(' ');
        foreach (string woord in woorden)
        {
            if (woord != "" && woord != " ")
            {
                string temp = woord;
                predicate = predicate.Or(g => g.TITEL.Contains(temp));
            }
            else
            {
                predicate = predicate.Or(g => g.TITEL != null); 
            }
        }
        return predicate;
    }
    public static Expression<Func<Maker, bool>> ContainsMaker(
                                               string keywords)
    {
        var predicate = PredicateBuilder.False<Maker>();
        string[] woorden = keywords.Split(' ');
        foreach (string woord in woorden)
        {
            if (woord != "" && woord != " ")
            {
                string temp = woord;
                predicate = predicate.Or(m => m.NAAM.Contains(temp));
            }
            else
            {
                predicate = predicate.Or(m => m.NAAM != null);
            }
        }
        return predicate;
    }

    public static Expression<Func<Platform, bool>> ContainsPlatform(
                                           string keywords)
    {
        var predicate = PredicateBuilder.False<Platform>();
        string[] woorden = keywords.Split(' ');
        foreach (string woord in woorden)
        {
            if (woord != "" && woord != " ")
            {
                string temp = woord;
                predicate = predicate.Or(p => p.OMSCHRIJVING.Contains(temp));
            }
            else
            {
                predicate = predicate.Or(p => p.OMSCHRIJVING != null);
            }
        }
        return predicate;
    }

    public static Expression<Func<Game, bool>> ContainsGrootte(
                                           string keywords)
    {
        var predicate = PredicateBuilder.False<Game>();
        string[] woorden = keywords.Split(' ');
        foreach (string woord in woorden)
        {
            if (woord != "" && woord != " ")
            {
                string temp = woord;
                predicate = predicate.Or(g => g.BESTANDSGROOTTEGAME <= (int.Parse(temp)));
            }
            else
            {
                predicate = predicate.Or(g => g.BESTANDSGROOTTEGAME != null);
            }
        }
        return predicate;
    }
}
